#coding=utf-8

import sys
fn = sys.argv[1]
data = eval( open(fn).read() )

groups = data['l']['']['x']

csv_output = "group,image,p0,p1,p2,j0,j1,j2,j3,j4,j5,j6,j7,j8\n";
for group_id in groups.keys():
    group = groups[group_id]
    group_data = group['r']
    
    keys = group_data.keys() 
    keys.sort(lambda x,y: eval(x)-eval(y))
    
    for k in keys:
        i = group_data[k]
        csv_output += group_id + ',' + k
        for p in i['p']:
            csv_output += ','+ str(p) 
        for j in i['j']:
            csv_output += ',' + str(j)
        csv_output += '\n'

open('./0.csv', 'w').write(csv_output)
